package dcl.software.filesync.server;

import javax.annotation.Resource;

import org.bson.Document;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

@Component
@DependsOn("mongoClient")
public abstract class BaseRepository {
	@Value("${mongo.database}")
	private String defaultDatabase;

	@Resource
	private MongoClient mongoClient;

	private MongoDatabase database;
	private String collection;

	protected String getDatabase() {
		return defaultDatabase;
	}

	protected abstract String getCollectionString();

	protected MongoCollection<Document> getCollectionSession() {
		if (database == null) {
			database = mongoClient.getDatabase(getDatabase());
			collection = getCollectionString();
		}
		return database.getCollection(collection);
	}
}
